package com.five.api.controller;


import com.five.api.service.LabelService;
import com.five.controller.BaseController;
import com.five.entity.Label;
import com.five.vo.BaseResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author star
 * @since 2021-06-16
 */
@RestController
@CrossOrigin(originPatterns = "*",allowCredentials = "true")
@RequestMapping("label")
@Api(tags = "标签接口")
public class LabelController extends BaseController {

    @Autowired
    private LabelService labelService;


    @ApiOperation(value = "新增标签接口")
    @PostMapping("insertLabel")
    public BaseResultVO insertLabel(@ApiParam("标签对象") Label label){

        if (label == null) {
            return errorResp("输入信息错误，请重新输入");
        }
        labelService.insertLabel(label.getUserId(),label.getLabelName());
        return successResp("添加标签成功");
    }

    @PostMapping("deleteLabel")
    @ApiOperation("删除标签接口")
    public BaseResultVO deleteLabel(@ApiParam("标签id") @RequestParam("id") Long id){
        System.out.println(id);
        if (id == null || id == 0){
            return errorResp("标签id不能为空");
        }
        labelService.deleteLabel(id);
        return successResp("删除标签成功");
    }

    @PostMapping("updateLabel")
    @ApiOperation(value = "修改标签接口")
    public BaseResultVO updateLabel(@ApiParam("标签对象") Label label){
        if (label == null){
            return errorResp("修改的标签对象不能为空");
        }
        labelService.updateLabel(label.getId(),label.getLabelName());
        return successResp("标签修改成功");
    }

    @PostMapping("selectLabelByUserId")
    @ApiOperation("查询用户的标签接口")
    public BaseResultVO selectLabelByUserId(@ApiParam("用户id") @RequestParam("userId") Long userId){
        System.out.println(userId);
        if (userId == null || userId == 0){
            return errorResp("用户id不能为空");
        }
        List<Label> labels = labelService.selectLabelByUserId(userId);
        return successResp(labels);
    }

}
